#include "tcStateOptions.h"
#include <GL/freeglut.h>
#include <juanmabc.h>
#include "tcGame.h"
#include "tcStateMain.h"

void
tcStateOptionsIn(void)
{
	TCGame *game;

	game = tcGame();

	tlGenStrings(5, game->options.strs);
}

void
tcStateOptionsOut(void)
{
	TCGame *game;

	game = tcGame();

	tlDeleteStrings(5, game->options.strs);
}

void
tcStateOptionsKeyboard(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateOptionsKeyboardUp(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	if(key == 27)
	{
		alSourcePlay(game->general.sounds[2]);
		tcStateOptionsOut();
		game->state = TC_STATE_MAIN;
		tcStateMainIn();
	}
}

void
tcStateOptionsSpecial(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	switch(key)
	{
		case GLUT_KEY_UP:
			alSourcePlay(game->general.sounds[0]);
			game->options.selection = (game->options.selection + 5 - 1) % 5;
		break;
		case GLUT_KEY_DOWN:
			alSourcePlay(game->general.sounds[0]);
			game->options.selection = (game->options.selection + 1) % 5;
		break;
		case GLUT_KEY_LEFT:
			switch(game->options.selection)
			{
				case 0:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.ntoys--;
					if(game->options.race.ntoys < 2)
						game->options.race.ntoys = 2;
					if(game->options.race.ntoys < game->options.race.nplayers)
						game->options.race.nplayers = game->options.race.ntoys;
				break;
				case 1:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.nplayers--;
					if(game->options.race.nplayers < 1)
						game->options.race.nplayers = 1;
				break;
				case 2:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.npoints--;
					if(game->options.race.npoints < 1)
						game->options.race.npoints = 1;
				break;
				case 3:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.speed -= 1.0f;
					if(game->options.race.speed < 5.0f)
						game->options.race.speed = 5.0f;
				break;
				case 4:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.difficulty -= 0.1f;
					if(game->options.race.difficulty < 0.5f)
						game->options.race.difficulty = 0.5f;
				break;
				default:
				break;
			}
		break;
		case GLUT_KEY_RIGHT:
			switch(game->options.selection)
			{
				case 0:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.ntoys++;
					if(game->options.race.ntoys > 8)
						game->options.race.ntoys = 8;
				break;
				case 1:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.nplayers++;
					if(game->options.race.nplayers > game->options.race.ntoys)
						game->options.race.nplayers = game->options.race.ntoys;
					if(game->options.race.nplayers > 4)
						game->options.race.nplayers = 4;
				break;
				case 2:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.npoints++;
					if(game->options.race.npoints > 10)
						game->options.race.npoints = 10;
				break;
				case 3:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.speed += 1.0f;
					if(game->options.race.speed > 30.0f)
						game->options.race.speed = 30.0f;
				break;
				case 4:
					alSourcePlay(game->general.sounds[1]);
					game->options.race.difficulty += 0.1f;
					if(game->options.race.difficulty > 1.0f)
						game->options.race.difficulty = 1.0f;
				break;
				default:
				break;
			}
		break;
		default:
		break;
	}
}

void
tcStateOptionsSpecialUp(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateOptionsFrame(void)
{
	TCGame *game;
	unsigned int i;
	unsigned int w, h;
	float tw, th;
	wchar_t buffer[128];

	game = tcGame();

	swprintf(buffer, 128, L"Toys:<%i>", game->options.race.ntoys);
	tlStringLoad(game->options.strs[0], buffer);
	swprintf(buffer, 128, L"Players:<%i>", game->options.race.nplayers);
	tlStringLoad(game->options.strs[1], buffer);
	swprintf(buffer, 128, L"Points:<%i>", game->options.race.npoints);
	tlStringLoad(game->options.strs[2], buffer);
	swprintf(buffer, 128, L"Speed:<%.1f>", game->options.race.speed);
	tlStringLoad(game->options.strs[3], buffer);
	swprintf(buffer, 128, L"Difficulty:<%.1f>", game->options.race.difficulty);
	tlStringLoad(game->options.strs[4], buffer);

	gluLookAt(0.0f, 0.0f, 250.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 

	tw = th = 0.0f;
	for(i = 0; i < 5; i++)
	{
		flFontStringMetrics(game->general.font, game->options.strs[i], &w, &h);
		tw = tw >= w ? tw : w;
		th += i < 5 - 1 ? (float) h * 1.5f : (float) h;
	}

	glPushMatrix();
	glScalef(0.75f * game->general.display.aspect, 0.75f * game->general.display.aspect, 0.75f * game->general.display.aspect);
	glTranslatef(0.0f, th / 2.0f, 0.0f);
	for(i = 0; i < 5; i++)
	{
		flFontStringMetrics(game->general.font, game->options.strs[i], &w, &h);

		glDisable(GL_CULL_FACE);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glMatrixMode(GL_TEXTURE);
		glPushMatrix();
		glScalef(0.1f, 0.1f, 0.1f);
		glMatrixMode(GL_MODELVIEW);

		mlMaterialBind(game->general.fontmat);

		glPushMatrix();
		glTranslatef(- (float) w / 2.0f, - (float) h, 0.0f);
		flFontDrawStringFill(game->general.font, game->options.strs[i]);
		glPopMatrix();

		glMatrixMode(GL_TEXTURE);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glEnable(GL_CULL_FACE);

		if(i == game->options.selection)
		{
			glPushMatrix();
			glTranslatef(- (float) w / 2.0f - 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef(- (float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();

			glPushMatrix();
			glTranslatef((float) w / 2.0f + 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef((float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();
		}

		glTranslatef(0.0f, - (float) h * 1.5f, 0.0f);

	}
	glPopMatrix();

	tlStringsUnload(5, game->options.strs);
}
